package com.example.demo.controller;

import com.example.demo.entity.ResultEntity;
import com.example.demo.service.serviceimpl.ProductsServiceImpl;
import com.example.demo.vo.ProductsVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/products/")
@CrossOrigin
@RestController
public class ProductsController {
    @Resource
    ProductsServiceImpl productsService;
    
    // 根据ID获取商品详情
    @GetMapping("getProductById")
    private ResultEntity getProductById(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(productsService.getProductById(id));
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("商品获取失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 获取所有商品
    @GetMapping("getAllProducts")
    private ResultEntity getAllProducts() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCode(0);
            resultEntity.setMsg("获取成功");
            resultEntity.setData(productsService.getAllProducts());
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("商品列表获取失败");
            e.printStackTrace();
        }
        return resultEntity;
    }
    

    
    // 添加商品
    @PostMapping("addProduct")
    private ResultEntity addProduct(@RequestBody ProductsVo productsVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (productsService.addProduct(productsVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("添加失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 更新商品
    @PutMapping("updateProduct")
    private ResultEntity updateProduct(@RequestBody ProductsVo productsVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (productsService.updateProduct(productsVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("修改失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
    
    // 删除商品
    @DeleteMapping("deleteProduct")
    private ResultEntity deleteProduct(@RequestParam("id") Integer id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (productsService.deleteProduct(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("删除失败");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("错误");
            e.printStackTrace();
        }
        return resultEntity;
    }
}
